/*
 * To change this license header, choose License Headers in Project Properties.
 * To change this template file, choose Tools | Templates
 * and open the template in the editor.
 */
package model.dao;

import java.sql.Connection;
import java.sql.PreparedStatement;
import java.sql.ResultSet;
import java.sql.SQLException;
import java.util.ArrayList;
import java.util.List;
import model.entity.PhongTro;
import model.entity.Quan;
import model.service.QuanDAOService;

/**
 *
 * @author duy
 */
public class QuanDAO implements QuanDAOService {

    private static QuanDAO quanDAO;

    public static QuanDAO getInstance() {
        if (quanDAO == null) {
            quanDAO = new QuanDAO();
        }
        return quanDAO;
    }

    @Override
    public List<Quan> getAllQuan() {

        List<Quan> list = new ArrayList<>();
        try {
            Connection conn = ConnectionFactory.getConnection();
            String sql = "select * from tbl_quan";
            PreparedStatement pstmt = conn.prepareStatement(sql);
            ResultSet rs = pstmt.executeQuery();
            while (rs.next()) {
                Quan item = new Quan();
                item.setMaquan(rs.getInt("maquan"));
                item.setTenquan(rs.getString("tenquan"));
                item.setGhichu(rs.getString("ghichu"));
                item.setIsActive(rs.getBoolean("isActive"));
                list.add(item);
            }
        } catch (SQLException | ClassNotFoundException e) {
            e.printStackTrace();
        }
        return list;

    }

    @Override
    public Quan getQuanbyMa(int maquan) {

        Quan item = new Quan();

        try {
            Connection conn = ConnectionFactory.getConnection();
            String sql = "select * from tbl_quan where maquan=? ";
            PreparedStatement pstmt = conn.prepareStatement(sql);
            pstmt.setInt(1, maquan);
            ResultSet rs = pstmt.executeQuery();
            while (rs.next()) {
                item.setMaquan(rs.getInt("maquan"));
                item.setTenquan(rs.getString("tenquan"));
                item.setGhichu(rs.getString("ghichu"));
                item.setIsActive(rs.getBoolean("isActive"));
               // List<PhongTro> phongtroList = PhongTroDAO.getInstance().getPhongTrobyQUan(rs.getInt("maquan"));
              //  item.setPhongtroList(phongtroList);
            }
        } catch (SQLException | ClassNotFoundException e) {
            e.printStackTrace();
        }

        return item;
    }

    @Override
    public boolean themmoiQuan(Quan quan) {
        throw new UnsupportedOperationException("Not supported yet."); //To change body of generated methods, choose Tools | Templates.
    }

    @Override
    public boolean suaQuan(Quan quan) {
        throw new UnsupportedOperationException("Not supported yet."); //To change body of generated methods, choose Tools | Templates.
    }

    @Override
    public boolean xoaQuan(Quan quan) {
        throw new UnsupportedOperationException("Not supported yet."); //To change body of generated methods, choose Tools | Templates.
    }

    @Override
    public boolean removeQuan(int maquan) {
        throw new UnsupportedOperationException("Not supported yet."); //To change body of generated methods, choose Tools | Templates.
    }

    @Override
    public boolean restoreQuan(int maquan) {
        throw new UnsupportedOperationException("Not supported yet."); //To change body of generated methods, choose Tools | Templates.
    }

    @Override
    public Quan getMabyQuan(String tenquan) {

        Quan item = new Quan();

        try {
            Connection conn = ConnectionFactory.getConnection();
            String sql = "select * from tbl_quan where tenquan=? ";
            PreparedStatement pstmt = conn.prepareStatement(sql);
            pstmt.setString(1, tenquan);
            ResultSet rs = pstmt.executeQuery();
            while (rs.next()) {
                item.setMaquan(rs.getInt("maquan"));
                item.setTenquan(rs.getString("tenquan"));
                item.setGhichu(rs.getString("ghichu"));
                item.setIsActive(rs.getBoolean("isActive"));
            }
        } catch (SQLException | ClassNotFoundException e) {
            e.printStackTrace();
        }

        return item;
    }

    public Quan getMabyQuan(int quan) {
        throw new UnsupportedOperationException("Not supported yet."); //To change body of generated methods, choose Tools | Templates.
    }

}
